﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UnrealConverterLogic
{
    public class Reference : IEquatable<Reference>
    {
        string SetName;
        double UnrealUnits;
        double MaxUnits;
        double Meters;

        Ratio Ratio;
        public Reference(string SetName, double UnrealUnits, double MaxUnits, double Meters, Ratio ratio)
        {
            this.SetName = SetName;
            this.UnrealUnits = UnrealUnits;
            this.MaxUnits = MaxUnits;
            this.Meters = Meters;
            this.Ratio = ratio;
        }
        public string GetSetName
        {
            get
            {
                return SetName;
            }
        }
        public double GetUnrealUnits
        {
            get
            {
                return UnrealUnits;
            }
        }
        public double GetMaxUnits
        {
            get
            {
                return MaxUnits;
            }
        }
        public double GetMeters
        {
            get
            {
                return Meters;
            }
        }
        public Ratio GetRatio
        {
            get
            {
                return Ratio;
            }
        }
        public string[] ToArray()
        {
            string[] str = new string[4];
            str[0] = SetName;
            str[1] = UnrealUnits.ToString();
            str[2] = MaxUnits.ToString();
            str[3] = Meters.ToString();
            return str;
        }

        public bool Equals(Reference other)
        {
            return
                (
                    this.SetName == other.SetName           &&
                    this.UnrealUnits == other.UnrealUnits   &&
                    this.MaxUnits == other.MaxUnits         &&
                    this.Meters == other.Meters             
                );
        }
    }
    public partial class UnrealConverter
    {
        private static List<Reference> references;

        public static void OnDeleteKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Delete)
            {
                DataGridView view = (DataGridView)sender;

                foreach (DataGridViewRow r in view.SelectedRows)
                {
                    references.Remove
                        (
                            new Reference
                            (
                                r.Cells[0].ToString(),
                                Double.Parse(r.Cells[1].ToString()),
                                Double.Parse(r.Cells[2].ToString()),
                                Double.Parse(r.Cells[3].ToString()),
                                null
                            )
                        );
                    dgvReferences.Rows.Clear();
                    InitReferences();
                }
            }
        }
        public static void InitReferences()
        {
            for (int i = 0; i < references.Count; i++)
            {
                dgvReferences.Rows.Insert(0, references[i].GetSetName, references[i].GetUnrealUnits.ToString(), references[i].GetMaxUnits.ToString(), references[i].GetMeters.ToString());
            }
        }
        private static void AddReference(Reference r)
        {
            references.Add(r);
            dgvReferences.Rows.Insert(0, r.GetSetName, r.GetUnrealUnits, r.GetMaxUnits, r.GetMeters);
        }
        public static void AddReference(string SetName, double UnrealUnits, double MaxUnits, double Meters)
        {
            references.Add(new Reference(SetName, UnrealUnits, MaxUnits, Meters, UsedRatio));
        }
        public static void AddCurrentSetToReference(object sender, EventArgs e)
        {
            if (dgvReferences != null)
            {
                if (InputFieldsNotEmpty())
                {
                    if (dgvReferences.Rows.Count == 1 && isRowEmpty())
                    {
                        for (int i = 0; i < dgvReferences.Rows[0].Cells.Count; i++)
                        {
                            if (i == 0) dgvReferences.Rows[0].Cells[i].Value = tbInputSetName.Text;
                            if (i == 1) dgvReferences.Rows[0].Cells[i].Value = tbInputUU.Text;
                            if (i == 2) dgvReferences.Rows[0].Cells[i].Value = tbInputMaxUnits.Text;
                            if (i == 3) dgvReferences.Rows[0].Cells[i].Value = tbInputMeters.Text;
                        }
                    }
                    else
                    {
                        if (!dgvReferences.Columns[0].Name.Equals("Item")) dgvReferences.Rows.Insert(0, tbInputSetName.Text, tbInputUU.Text, tbInputMaxUnits.Text, tbInputMeters.Text);
                        else dgvReferences.Rows.Insert(0, "", tbInputSetName.Text, tbInputUU.Text, tbInputMaxUnits.Text, tbInputMeters.Text);
                    }
                }
            }
            references.Add(CreateReferenceRow());
            //ParseProjectTags();
        }
        private static Reference CreateReferenceRow()
        {
            string SetName = tbInputSetName.Text;
            double UnrealUnits = Double.Parse(tbInputUU.Text);
            double MaxUnits = Double.Parse(tbInputMaxUnits.Text);
            double Meters = Double.Parse(tbInputMeters.Text);

            Reference reference = new Reference(SetName, UnrealUnits, MaxUnits, Meters, UsedRatio);
            return reference;
        }
        public static void OnDataGridViewDeletePressed(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DataGridView dgv = (DataGridView)sender;

                foreach (DataGridViewRow row in dgv.Rows)
                {
                    if (row.Selected)
                    {
                        double dUU      = row.Cells[1].Value.GetType() == typeof(double) ? (double)row.Cells[1].Value : Double.Parse((string)row.Cells[1].Value);
                        double dMax     = row.Cells[2].Value.GetType() == typeof(double) ? (double)row.Cells[2].Value : Double.Parse((string)row.Cells[2].Value);
                        double dMeters  = row.Cells[3].Value.GetType() == typeof(double) ? (double)row.Cells[3].Value : Double.Parse((string)row.Cells[3].Value);
                        Reference reference = new Reference
                            (
                                (string)row.Cells[0].Value, 
                                dUU,
                                dMax,
                                dMeters,
                                null
                            );
                        references.Remove(reference);
                    }
                }
            }
        }
        public static void OnRowDoubleClicked(object sender, EventArgs e)
        {
            if (dgvReferences.CurrentRow.Selected)
            {
                /*
                if (references[dgvReferences.CurrentRow.Index].GetRatio != null)
                {
                    tbInputSetName.Text = (string)dgvReferences.CurrentRow.Cells[0].Value;
                    UsedRatio = references[dgvReferences.CurrentRow.Index].GetRatio;
                    UpdateRatios();
                    CurrentConversion.UnrealUnits = references[dgvReferences.CurrentRow.Index].GetUnrealUnits;
                    CurrentConversion.MaxUnits = references[dgvReferences.CurrentRow.Index].GetMaxUnits;
                    CurrentConversion.Meters = references[dgvReferences.CurrentRow.Index].GetMeters;
                }
                 * */
            }
            if (dgvReferences.CurrentRow.Cells[0].Selected && !dgvReferences.CurrentRow.Selected)
            {
                tbInputSetName.Text = (string)dgvReferences.CurrentRow.Cells[0].Value;
            }
            if (dgvReferences.CurrentRow.Cells[1].Selected && !dgvReferences.CurrentRow.Selected)
            {
                LastFocusedInputField = InputField.UNREAL;
                UpdateLabelColors(LastFocusedInputField);
                tbInputUU.Text = dgvReferences.CurrentRow.Cells[1].Value.ToString();
                UpdateMathFields(Units.UNREAL_UNITS);
                
            }
            if (dgvReferences.CurrentRow.Cells[2].Selected && !dgvReferences.CurrentRow.Selected)
            {
                LastFocusedInputField = InputField.MAX;
                UpdateLabelColors(LastFocusedInputField);
                tbInputMaxUnits.Text = dgvReferences.CurrentRow.Cells[2].Value.ToString();
                UpdateMathFields(Units.MAX_UNITS);
                
            }
            if (dgvReferences.CurrentRow.Cells[3].Selected && !dgvReferences.CurrentRow.Selected)
            {
                LastFocusedInputField = InputField.METERS;
                UpdateLabelColors(LastFocusedInputField);
                tbInputMeters.Text = dgvReferences.CurrentRow.Cells[3].Value.ToString();
                UpdateMathFields(Units.METERS);
            }
        }
        private static bool isRowEmpty()
        {
            for (int i = 0; i < dgvReferences.Rows[0].Cells.Count; i++)
            {
                if (dgvReferences.Rows[0].Cells[i].Value == null) return false;
            }
            return true;
        }
        private static void ParseProjectTags()
        {
            if (dgvReferences.Rows.Count > 3)
            {
                string[] setnames = new string[dgvReferences.Rows.Count];
                for (int i = 0; i < dgvReferences.Rows.Count - 1; i++)
                {
                    if (dgvReferences.Columns[0].Name != "Item")
                        setnames[i] = dgvReferences.Rows[i].Cells[0].Value.ToString();
                    else
                        setnames[i] = dgvReferences.Rows[i].Cells[1].Value.ToString();
                }

                int num = 0;
                string projecttag1;
                string projecttag2;
                string finalprojecttag;
                if (setnames[num] != "" &&
                    setnames[num + 1] != "" &&
                    setnames[num + 2] != ""
                )
                {
                    LongestCommonSubstring(setnames[num], setnames[num + 1], out projecttag1);
                    LongestCommonSubstring(setnames[num + 1], setnames[num + 2], out projecttag2);
                    LongestCommonSubstring(projecttag1, projecttag2, out finalprojecttag);

                    if (finalprojecttag != "")
                    {
                        if (!dgvReferences.Columns[0].Name.Contains("Item"))
                        {
                            DataGridViewColumn column = new DataGridViewColumn(dgvReferences.Columns[0].CellTemplate);
                            column.Name = "Item";
                            column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                            dgvReferences.Columns.Insert(0, column);


                            dgvReferences.Rows[num].Cells[1].Value = dgvReferences.Rows[num].Cells[1].Value.ToString().Replace(finalprojecttag, "");
                            dgvReferences.Rows[num + 1].Cells[1].Value = dgvReferences.Rows[num + 1].Cells[1].Value.ToString().Replace(finalprojecttag, "");
                            dgvReferences.Rows[num + 2].Cells[1].Value = dgvReferences.Rows[num + 2].Cells[1].Value.ToString().Replace(finalprojecttag, "");
                            dgvReferences.Rows[num + 1].Cells[0].Value = finalprojecttag;
                        }
                        else
                        {
                            dgvReferences.Rows[num].Cells[1].Value = dgvReferences.Rows[num].Cells[1].Value.ToString().Replace(finalprojecttag, "");
                            dgvReferences.Rows[num + 1].Cells[1].Value = dgvReferences.Rows[num + 1].Cells[1].Value.ToString().Replace(finalprojecttag, "");
                            dgvReferences.Rows[num + 2].Cells[1].Value = dgvReferences.Rows[num + 2].Cells[1].Value.ToString().Replace(finalprojecttag, "");
                            dgvReferences.Rows[num + 1].Cells[0].Value = finalprojecttag;
                        }
                    }
                    num += 3;
                }
            }
        }
    }
}
